package com.hkfrich.system.security.service.impl;


import com.hkfrich.common.exception.UserException;
import com.hkfrich.system.domain.SysUser;
import com.hkfrich.system.security.domain.SecurityUserDetails;
import com.hkfrich.system.service.SysMenuService;
import com.hkfrich.system.service.SysUserService;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @ClassName UserDetailsService
 * @Desscription
 * @Author _HeiShuai
 * @Date 2022/8/18 11:37
 * @Version 1.0
 **/
@Service
public class SecurityUserDetailsServiceImpl implements UserDetailsService {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private SysMenuService sysMenuSevice;

    /**
     * @return
     * @Author _HeiShuai
     * @Description // 权限控制
     * @Date 11:40 2022/8/18
     * @Param
     **/
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        //TODO 查询用户信息
        SysUser sysUserVo = sysUserService.selectUserVoByUserName(userName);
        if (Objects.isNull(sysUserVo)) {
            throw new UserException("用户不存在");
        }
        List<String> permissions = sysMenuSevice.selectPermsByUserId(sysUserVo.getUserId());
        return new SecurityUserDetails(sysUserVo,permissions);
    }


}
